package com.work.keep.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.work.keep.R;
import com.work.keep.activity.DynamicActivity;
import com.work.keep.activity.HomePageActivity;
import com.work.keep.adapter.MyFragmentPagerAdapter;
import com.work.keep.adapter.OnColorChangable;
import com.work.keep.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/21.
 */
public class DynamicFragment extends BaseFragment implements View.OnClickListener, OnColorChangable {
    private Context context;
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private MyFragmentPagerAdapter myFragmentPagerAdapter;
    private List<Fragment> fragments;
    private DiscoverFragment discoverFragment;
    private FollowsFragment followsFragment;
    private ImageView dynamicFragmentTopLeft, dynamicFragmentTopRight;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_new, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewPager = (ViewPager) view.findViewById(R.id.viewPager);
        tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
        dynamicFragmentTopLeft = (ImageView) view.findViewById(R.id.dynamicFragmentTopLeft);
        dynamicFragmentTopRight = (ImageView) view.findViewById(R.id.dynamicFragmentTopRight);
    }

    @Override
    protected void init() {
        dynamicFragmentTopLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        dynamicFragmentTopRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        initViewPager();
    }

    private void initViewPager() {
        fragments = new ArrayList<>();
        discoverFragment = new DiscoverFragment();
        followsFragment = new FollowsFragment();
        fragments.add(discoverFragment);
        fragments.add(followsFragment);
        fragments.add(new NewestFragment());
        myFragmentPagerAdapter = new MyFragmentPagerAdapter(((HomePageActivity) context).getSupportFragmentManager(), fragments, context);
        viewPager.setAdapter(myFragmentPagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
        for (int i = 0; i < fragments.size(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            tab.setCustomView(myFragmentPagerAdapter.getTabView(i));
        }

        if (tabLayout.getTabAt(0).isSelected()) {
            viewPager.setCurrentItem(0);
            discoverFragment.setOnColorChangable(DynamicFragment.this);
        }


        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                viewPager.setCurrentItem(position);
                if (position == 1) {
                    followsFragment.setOnColorChangable(DynamicFragment.this);
                }

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                Log.d("GG", "un");
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                Log.d("GG", "re");
            }
        });
    }


    @Override
    public void onClick(View v) {
        Toast.makeText(context, "Floating", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void changeColor(int color) {
        tabLayout.setBackgroundColor(color);
    }
}
